<template>
    <div class="system-resource-container">
        <el-dialog title="新增开放资源" v-model="isShowDialog" >
            <el-form ref="addResourceFormRef" :model="formData" :rules="formRules" label-width="80px">
                <el-row :gutter="35">
                    <el-col>
                        <el-form-item label="上级资源">
                            <el-cascader
								placeholder="请选择上级开放资源信息（可选填）"
								v-model="formData.parentId"
								:options="parentOptions"
								:props="{expandTrigger: 'hover', emitPath: false, checkStrictly: true, value: 'id', label: 'name', children: 'children' }"
								class="w100"
								clearable>
                            </el-cascader>
                        </el-form-item>
                    </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="资源类型" prop="type">
							<el-radio v-model="formData.type" label="GROUP" clearable>分 组</el-radio>
							<el-radio v-model="formData.type" label="RESOURCE" clearable>资 源</el-radio>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="资源归属" prop="belong">
							<el-radio v-model="formData.belong" label="AUTH" clearable>系统</el-radio>
                            <el-radio v-model="formData.belong" label="COMMON" clearable>公共</el-radio>
							<el-radio v-model="formData.belong" label="OPEN" clearable>开放</el-radio>
						</el-form-item>
					</el-col>
                    <el-col>
                        <el-form-item label="资源名称" prop="name">
                            <el-input v-model="formData.name" placeholder="请输入资源名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="模块名称" prop="module">
                            <el-input v-model="formData.module" placeholder="请输入模块名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="权限字符" prop="perms">
                            <el-input v-model="formData.perms" placeholder="请输入权限字符" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="资源路径" prop="path">
                            <el-input v-model="formData.path" :disabled="formData.type === 'GROUP'" placeholder="请输入资源路径，例：/api-open/query/{id} 允许占位符方式“{id}”" clearable></el-input>
						</el-form-item>
                    </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="资源状态" prop="status">
							<el-select v-model="formData.status" placeholder="请选择资源状态" clearable class="w100">
								<el-option label="正常" value="0"></el-option>
								<el-option label="停用" value="1"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="资源排序" prop="orderNum">
							<el-input-number v-model="formData.orderNum" :min="0" :max="999" clearable class="w100"></el-input-number>
						</el-form-item>
					</el-col>
                    <el-col>
                        <el-form-item label="资源描述" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入资源描述" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getResourceList, addResource } from '/@/api/system/resource/index';
import { lineToTreeData } from '/@/utils/commonFunction';
export default {
    name: 'system/resource/add',
    setup(props, context) {
        const addResourceFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            parentOptions: [],
            formData: {
                parentId: 'root',
				type: 'RESOURCE',
				belong: 'AUTH',
				mode: '',
				modeKey: '',
				status: '0',
				orderNum: 1,
            },
            formRules : {
				type: { required: true, message: '资源类型不能为空', trigger: 'change' },
				belong: { required: true, message: '资源归属不能为空', trigger: 'change' },
                name: { required: true, message: '资源名称不能为空', trigger: 'blur' },
            }
        });

        //初始化上级级联
        const loadParentOptions = async () => {
            await getResourceList({type: 'GROUP'}).then((res) => {
                if(res.code == 0){
                    state.parentOptions = lineToTreeData(res.data);
                }
            });
        }
        
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
            loadParentOptions();
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            state.formData.parentId = 'root';
            addResourceFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            addResourceFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        parentId: state.formData.parentId,
                        type: state.formData.type,
                        name: state.formData.name,
                        module: state.formData.module,
                        perms: state.formData.perms,
                        path: state.formData.path,
						belong: state.formData.belong,
						mode: state.formData.mode,
						modeKey: state.formData.modeKey,
                        status: state.formData.status,
                        orderNum: state.formData.orderNum,
                        remark: state.formData.remark,
                    }
                    addResource(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            addResourceFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state),
        };
    },
};
</script>